package com.ebelter.myehc.ui.fragments.trends;



import com.ebelter.btcomlib.bases.fragment.BaseFragment;

import com.ebelter.btcomlib.models.bluetooth.products.ear_temperature.EarTemperatureManager;

import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.CommonUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.TimeUtils;
import com.ebelter.btcomlib.views.ChartView3;
import com.ebelter.btcomlib.views.CombinView7;
import com.ebelter.btcomlib.views.CombinView8;
import com.ebelter.myehc.R;

import com.ebelter.myehc.models.http.response.device_resports.Temperature_R;
import com.ebelter.myehc.utils.EHCCommonUtils;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ebelter.btcomlib.bases.activity.BaseActivity.getString_;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8SrcRes;
import static com.ebelter.myehc.ui.fragments.trends.Trend_XueYa_Fragment.setCb8Title;

/**
 * 趋势血压
 */

public class Trend_TiWen_Fragment extends BaseFragment {
    public static final String TAG = "Trend_TiWen_Fragment";
    private static final int[] levelColors = {0xff18d5e5, 0xff14e34e, 0xffffcc00, 0xffff9000, 0xfffc3838, 0xffaa0c0c};

    CombinView7 tw_cb7;
    CombinView8 tw_tw_cb8;
    ChartView3 chartView3;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_trend_tiwen;
    }


    public void setReportData(Temperature_R reportData) {
        if (reportData == null || reportData.resultCode != 1 || reportData.resultData == null) {
            return;
        }

        if (reportData.resultData.summaryList != null && reportData.resultData.summaryList.size() > 0) {
            setCb8Title(tw_tw_cb8, 2, reportData.resultData.summaryList.get(0).temperatureMin + "");
            setCb8Title(tw_tw_cb8, 3, reportData.resultData.summaryList.get(0).temperatureMax + "");
            setCb8Title(tw_tw_cb8, 4, reportData.resultData.summaryList.get(0).temperatureAvg + "");


        }


        //y轴坐标对应的数据
        List<Float> xyYValue = new ArrayList<>();
        for (int i = 0; i < 7; i++) {//Y轴的刻度值
            xyYValue.add((float) (i * 9));
        }
        //x轴坐标对应的数据
        List<String> oneXValue = new ArrayList<>();
        List<String> displayXValue = new ArrayList<>();
        List<Integer> xPointColor = new ArrayList<>();

        Map<String, Float> xyValue = new HashMap<>();


        float newestTem = 0;
        if (reportData.resultData.chartList != null) {

            //dateType:日期类型 1:年 2:月 3周: 4:天
            //measureType:1:血糖 2:血压 3:血氧 4:体温 5:尿酸 6:总胆固醇 7:人体成份
            for (int i = 0; i < reportData.resultData.chartList.size(); i++) {
                Temperature_R.ResultDataBean.ChartListBean data = reportData.resultData.chartList.get(i);


                if (data.temperatureAvg == 0) {
                    continue;
                }
                String timeParam = "";
                String disTimeParam = "";
                if (reportData.resultData.dateType == 1) {
                    Date date = TimeUtils.parseFormatter91Time(data.timeParam);
                    timeParam = CommonUtils.getMonths(NumUtils.string2Int(TimeUtils.formatTime12(date.getTime())));
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 2) {
                    timeParam = String.format(getString_(R.string.Week_int),data.timeParam);
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 3) {
                    Date date2 = TimeUtils.parseFormatter3Time(data.timeParam);
                    timeParam = TimeUtils.formatTime11(date2.getTime());
                    disTimeParam=timeParam;
                } else if (reportData.resultData.dateType == 4) {
                    Date date3 = TimeUtils.parseFormatter1Time(data.timeParam);
                    timeParam = TimeUtils.formatTime13(date3.getTime());
                    disTimeParam = TimeUtils.formatTime4(date3.getTime());
                }

                oneXValue.add(timeParam);
                displayXValue.add(disTimeParam);
                xyValue.put(timeParam, data.temperatureAvg);
                newestTem = data.temperatureAvg;
                xPointColor.add(levelColors[EarTemperatureManager.getResultLevel(data.temperatureAvg)]);

            }
        }

        if (reportData.resultData.summaryList != null && reportData.resultData.summaryList.size() > 0) {
            setCb8Title(tw_tw_cb8, 2, NumUtils.disFlZero(reportData.resultData.summaryList.get(0).temperatureMin,N2));
            setCb8Title(tw_tw_cb8, 3, NumUtils.disFlZero(reportData.resultData.summaryList.get(0).temperatureMax ,N2));
            setCb8Title(tw_tw_cb8, 4, NumUtils.disFlZero(reportData.resultData.summaryList.get(0).temperatureAvg ,N2));
            setCb8SrcRes(tw_tw_cb8, EHCCommonUtils.getDisplaySrcRes(reportData.resultData.summaryList.get(0).temperatureAvg, newestTem));
        }

        if (chartView3 != null) {
            chartView3.setxPointColor(xPointColor);
            chartView3.setdisXValue(displayXValue);
            chartView3.setValue(xyValue, oneXValue, xyYValue);
            chartView3.src2end();
        }


    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    private void FV() {
        tw_tw_cb8 = mRootView.findViewById(R.id.tw_tw_cb8);
        tw_cb7 = mRootView.findViewById(R.id.tw_cb7);
        if (AppUtils.en==AppUtils.getLocalStr()){
            tw_cb7.setTitleSizes(13);
            tw_tw_cb8.setTitleSizes(14);
        }
        chartView3 = mRootView.findViewById(R.id.trend_tw_tw_chartview3);

        chartView3.warringTem = 1000;
        chartView3.yOriStart = 25;

    }
}
